package com.securitydemo.controller;

import com.securitydemo.config.security.provider.HYAbstractAuthenticationToken;
import com.securitydemo.converter.UserConverter;
import com.securitydemo.dto.OffsetDateTimeDto;
import com.securitydemo.dto.UserKeyDto;
import com.securitydemo.dto.UserRegisterDto;
import com.securitydemo.config.security.provider.HYAbstractUserDetailsAuthenticationProvider;
import com.securitydemo.securitylogin.JwtAuthenticationSuccessHandler;
import com.securitydemo.securitylogin.LoginUser;
import com.securitydemo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

@RestController
@RequestMapping("/")
@Api(value = "登录", tags = {"登录"})
public class LoginController {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private IUserService userService;

    @Autowired
    private JwtAuthenticationSuccessHandler jwtAuthenticationSuccessHandler;

    @Autowired
    private HYAbstractUserDetailsAuthenticationProvider hyAbstractUserDetailsAuthenticationProvider;


    @PostMapping("/login")
    @ApiOperation("登录入口")
    private String login(String username, String password) {
        // 生成一个包含账号密码的认证信息
        Authentication token = new UsernamePasswordAuthenticationToken(username, password);
        // AuthenticationManager校验这个认证信息，返回一个已认证的Authentication
        Authentication authentication = authenticationManager.authenticate(token);
        // 将返回的Authentication存到上下文中
        SecurityContextHolder.getContext().setAuthentication(authentication);
        return "登录成功";
    }


    @PostMapping("/login/token")
    @ApiOperation("token登录入口")
    private String loginToken(String tokenStr) {
        // 生成一个包含账号密码的认证信息
        Authentication token = new HYAbstractAuthenticationToken(null, tokenStr);
        // AuthenticationManager校验这个认证信息，返回一个已认证的Authentication
        Authentication authentication = authenticationManager.authenticate(token);
        // 将返回的Authentication存到上下文中
        SecurityContextHolder.getContext().setAuthentication(authentication);
        return "登录成功";
    }

    @PostMapping("/getuser")
    @ApiOperation("获取当前登录的user数据")
    private LoginUser getUser(String userName, String password) {
        if(1==1){
            throw new RuntimeException("");
        }
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (LoginUser) authentication.getPrincipal();
    }


    @PostMapping("/register")
    @ApiOperation("用户注册")
    public String registerLogin(@RequestBody UserRegisterDto dto, UserKeyDto userKeyDto) {
        return userService.registerLogin(UserConverter.to(dto));
    }


    @PostMapping("/upload")
    @ApiOperation("上传用户数据")
    public String upload(@RequestParam("file") MultipartFile file) {
        long size1 = file.getSize();
        try (InputStream inputStream = file.getInputStream()){
            ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
            FileUtil.copyStream(inputStream,outputStream);
            int size = outputStream.size();
        }catch (Exception e){
            e.printStackTrace();
        }
        return String.valueOf(size1);
    }


    @GetMapping("/time")
    @ApiOperation("测试时间转换器")
    private OffsetDateTimeDto getTime(){
        return  OffsetDateTimeDto.info();
    }
}
